﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tjyyjl.Core.Dao;
using SqlSugar;
using EyFramework.ORM;
using ShuJuGj.Entity;
using ShuJuGj.Core;
using EyFramework.Common;
using Newtonsoft.Json;

namespace tjyyjl.Core.Service
{
    /// <summary>
    ///  TJ_TJYYJL表的服务实现类
    /// </summary>
    public class TjTjyyjlService : BaseTjService, ITjTjyyjlService
    {
        protected readonly ITjTjyyjlDao _TjTjyyjlDao;

        public TjTjyyjlService(ITjTjyyjlDao tjTjyyjlDao)
        {
            _TjTjyyjlDao = tjTjyyjlDao;
        }

        public List<TjTjyyjl> GetListBy(Inquire inquire)
        {
            return _TjTjyyjlDao.GetListBy(inquire);
        }

        public bool Insert(List<TjTjyyjl> results)
        {
            return _TjTjyyjlDao.Insert(results);
        }

        public bool updateState(TjTjyyjl tjTjyyjl)
        {
            return _TjTjyyjlDao.updateState(tjTjyyjl);
        }

        public bool Complete(string token, string serialNumber)
        {
            var url = ShuJuGjConstants.TJYY_URL + ShuJuGjConstants.TJYY_COMPLETE;
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("serialNumber", serialNumber);
            dict.Add("arriveDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            var json = JsonConvert.SerializeObject(dict);
            Logger.Info(json);
            //加密json数据
            var result = AESHelper.AESEncrypt(json, ShuJuGjConstants.TJYY_AESKEY);
            //回调数据
            var hint = Interface.PostGet(url, token, result);
            Logger.Info(hint);
            hint = AESHelper.AESDecrypt(hint, ShuJuGjConstants.TJYY_AESKEY);
            Logger.Info(hint);
            var stream = JsonConvert.DeserializeObject<ResponseData>(hint);
            if (stream.code != 200)
            {
                Globals.ShowError(stream.message + "!" + stream.code);
                return false;
                //数据错误
            }
            return true;
        }

        public bool Complete_TX(string token, string serialNumber)
        {
            var url = ShuJuGjConstants.TXTJYY_URL + ShuJuGjConstants.TXTJYY_COMPLETE;
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("serialNumber", serialNumber);
            dict.Add("arriveDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            var json = JsonConvert.SerializeObject(dict);
            Logger.Info(json);
            //加密json数据
            var result = AESHelper.AESEncrypt(json, ShuJuGjConstants.TXTJYY_AESKEY);
            //回调数据
            var hint = Interface.PostGet(url, token, result);
            Logger.Info(hint);
            hint = Interface.Test_data(hint);
            Logger.Info(hint);
            var stream = JsonConvert.DeserializeObject<ResponseData>(hint);
            if (stream.code != 200)
            {
                Globals.ShowError(stream.message + "!" + stream.code);
                return false;
                //数据错误
            }
            return true;
        }

        public string GetData(String token, String body)
        {
            var url = ShuJuGjConstants.TJYY_URL + ShuJuGjConstants.TJYY_GETURL;
            var json = Interface.PostGet(url, token, body);
            json = AESHelper.AESDecrypt(json, ShuJuGjConstants.TJYY_AESKEY);
            Logger.Info(json);
            return json;
        }

        public string GetData_TX(string token, string body)
        {
            var url = ShuJuGjConstants.TXTJYY_URL + ShuJuGjConstants.TXTJYY_GETURL;
            var json = Interface.PostGet(url, token, body);
            json = Interface.Test_data(json);
            Logger.Info(json);
            return json;
        }

        public String GetToken()
        {
            String url = ShuJuGjConstants.TJYY_URL + ShuJuGjConstants.TJYY_TOKENURL;
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("apikey", ShuJuGjConstants.TJYY_APIKEY);
            dict.Add("secret", ShuJuGjConstants.TJYY_SECRET);
            var json = JsonConvert.SerializeObject(dict);
            Logger.Info(json);
            //加密json数据
            var result = AESHelper.AESEncrypt(json, ShuJuGjConstants.TJYY_AESKEY);

            //获取浙里办信息,得到加密的token
            String tokenList = Interface.PostGetToken(url, result);
            Logger.Info(tokenList);
            //解密token的json文件
            tokenList = AESHelper.AESDecrypt(tokenList, ShuJuGjConstants.TJYY_AESKEY);
            Logger.Info(tokenList);
            tokenList = Interface.ChangeJson(tokenList);
            return tokenList;
        }

        public string GetToken_TX()
        {
            String url = ShuJuGjConstants.TXTJYY_URL + ShuJuGjConstants.TXTJYY_TOKENURL;
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("apikey", ShuJuGjConstants.TXTJYY_APIKEY);
            dict.Add("secret", ShuJuGjConstants.TXTJYY_SECRET);
            var json = JsonConvert.SerializeObject(dict);
            Logger.Info(json);
            //加密json数据
            var result = AESHelper.AESEncrypt(json, ShuJuGjConstants.TXTJYY_AESKEY);

            //获取浙里办信息,得到加密的token
            String tokenList = Interface.PostGetToken(url, result);
            Logger.Info(tokenList);
            //解密token的json文件
            tokenList = Interface.Test_data(tokenList);
            Logger.Info(tokenList);
            tokenList = Interface.ChangeJson(tokenList);
            return tokenList;
        }

        public void SetData(string token, string requestId, string serialNumber)
        {
            var url = ShuJuGjConstants.TJYY_URL + ShuJuGjConstants.TJYY_SETURL;
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("requestId", requestId);
            dict.Add("serialNumber", serialNumber);
            var json = JsonConvert.SerializeObject(dict);
            //加密json数据
            var result = AESHelper.AESEncrypt(json, ShuJuGjConstants.TJYY_AESKEY);
            //回调数据
            var hint = Interface.PostGet(url, token, result);
            hint = AESHelper.AESDecrypt(hint, ShuJuGjConstants.TJYY_AESKEY);
            var stream = JsonConvert.DeserializeObject<ResponseData>(hint);
            if (stream.code == 200)
            {
                //成功
                Globals.ShowTips(stream.message);
            }
            else
            {
                //数据错误
                Globals.ShowError(stream.message + "!" + stream.code);
            }
        }

        public void SetData_TX(string token, string requestId, string serialNumber)
        {
            var url = ShuJuGjConstants.TXTJYY_URL + ShuJuGjConstants.TXTJYY_SETURL;
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("requestId", requestId);
            dict.Add("serialNumber", serialNumber);
            var json = JsonConvert.SerializeObject(dict);
            //加密json数据
            var result = AESHelper.AESEncrypt(json, ShuJuGjConstants.TXTJYY_AESKEY);
            //回调数据
            var hint = Interface.PostGet(url, token, result);
            hint = Interface.Test_data(hint);
            var stream = JsonConvert.DeserializeObject<ResponseData>(hint);
            if (stream.code == 200)
            {
                //成功
                Globals.ShowTips(stream.message);
            }
            else
            {
                //数据错误
                Globals.ShowError(stream.message + "!" + stream.code);
            }
        }
    }
}